package day26;

import day7.Phone;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//反射实现通用方法的调用
public class Demo5 {
    public static Object base(Object o,String name,Object ... params) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class c = o.getClass();
        Class[] args = new Class[params.length];
        for(int i=0;i<params.length;i++) {
            args[i] = params[i].getClass();
        }
        Method m = c.getDeclaredMethod(name,args);
        m.setAccessible(true);
        Object result = m.invoke(o,params);
        return result;
    }
    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        Phone p = new Phone();
        Object res = Demo5.base(p,"setType","红魔");
        System.out.println(res);
        
    }
}
